<?php

namespace Home\Controller;

use Think\Controller;

class NoticeController extends Controller {

    /**
     * 包裹微信通知
     * @author cheng
     */
    public function noticeInfo() {

        $Notice = new \Home\Server\ParcelNoticeResultServer();
        $result = $Notice->sendWeixin();

//        var_dump($result);die;
        foreach ($result as $k => $v)
        {
            $codeData = $v['f_take_code'];

            //如果是专业版, 则提货码 变为编号
            if( ( new \Home\Server\UserService() ) ->verify_professional( $v['f_station_id'] ) || $v['f_speed_status'] == '2' )
            {
                //兼容老数据  过后可以弃用
                //获取单条包裹信息
//                $oneData = ( new \Home\Server\QueryParcelService() ) ->getOneParcel_Data( $v['f_parcel_id'] );

                $codeData = $v['f_num'] . ' (包裹编号)';
//                $codeData = $oneData['f_num'] . ' (包裹编号)';
            }

            $mailNo = $v['f_mail_no'];

            //运单号为空则推送包裹id （极速版运单号为空）
            if ( $v['f_mail_no'] == '' )
            {
				$mailNo = $v['f_parcel_id'].'(包裹ID)';
            }

            //发微信并记录发送微信次数，根据返回值修改微信通知状态或成功发送时间
            $data = array(
                'mobile'    => $v['f_recipient_mobile'], //收件人手机号码
                'name'      => $v['f_station_name'],     //站点名称
                'mailno'    => $mailNo,                  //运单号
                'code'      => $codeData,                //提货码
                'company'   => $v['f_company_name'],     //快递公司名称
                'address'   => $v['f_address'],          //站点地址
                'qtime'     => $v['f_qtime'],            //站点营业时间
                'remark'    => '感谢您的使用',
                'type'      => $v['f_speed_status'],     //入库类型  0.普通版  1.专业版  2.极速版  2017-5-23 quyang
                'tel'       => $v['f_tel']               //站点的联系电话
            );
            $return_weixin = send_weixin($data);
            $arr_weixin = json_decode($return_weixin, true);

            $Notice->updateParcelNotice( $v['f_parcel_id'] , ['f_weixin_num'=>['exp','f_weixin_num+1']] , $v['table_name'] );

            $n_data['f_parcel_id'] = $v['f_parcel_id'];
            $n_data['f_weixin_send_time'] = date('YmdHis');
            $n_data['f_weixin_send_time_stamp'] = time();
            if ($arr_weixin['status'] == 1) {
                $n_data['f_weixin_notice'] = 2; //失败
            } else {
                $n_data['f_weixin_notice'] = 3; //已发送
            }
            $Notice->updateParcelNotice( $v['f_parcel_id'] , $n_data , $v['table_name'] );

            //输出推送的手机号码
            echo $v['f_recipient_mobile'] . ',';
        }
    }
    
    /**
     * 定时任务调用把包裹表里的微信、短信通知状态失败的数据插入到tp_parcel_notice_result表
     * @again quyang 2017-1-21
     * @author cheng
     */
    public function copyParcelToNotice() {
        echo  '本方法废弃';
        exit;
        //早上0点到早上6点不执行
        $now = time();
        $stime = strtotime(date('Ymd'));
        $etime = strtotime(date('Ymd'))+6*60*60;
        if ($now > $stime && $now < $etime) {
            exit();
        }
        //获取微信、微信通知状态失败的数据
        $parcelDetail = new \Home\Server\QueryParcelService();
        $result = $parcelDetail->queueParcelDetails();

        $IndexDataNUM = $IndexArrNUM = 0;

        foreach ($result as $k => $v) {
            $info = D('Home/ParcelNoticeResult')->where(array('f_parcel_id' => $v['f_parcel_id']))->find();
            if (empty($info)) {
                $data[$IndexDataNUM]['f_parcel_id'] = $v['f_parcel_id'];
                $data[$IndexDataNUM]['f_station_id'] = $v['f_station_id']; //新加字段
                $data[$IndexDataNUM]['f_recipient_mobile'] = $v['f_recipient_mobile'];
                $data[$IndexDataNUM]['f_recipient_name'] = $v['f_recipient_name'] ? $v['f_recipient_name'] : '';
                $data[$IndexDataNUM]['f_mail_no'] = $v['f_mail_no'];
                $data[$IndexDataNUM]['f_take_code'] = $v['f_take_code'];
                $data[$IndexDataNUM]['f_company_name'] = $v['f_company_name'];
                $data[$IndexDataNUM]['f_address'] = $v['f_address'] ? $v['f_address'] : '';
                $data[$IndexDataNUM]['f_qtime'] = $v['f_store_time'] ? $v['f_store_time'] : '';
                $data[$IndexDataNUM]['f_tel'] = $v['f_user_phone'] ? $v['f_user_phone'] : '';
                $data[$IndexDataNUM]['f_station_name'] = $v['f_station_name'] ? $v['f_station_name'] : '';
                $data[$IndexDataNUM]['f_create_time'] = date('YmdHis');

                $IndexDataNUM++;
            }
            //快递公司编号为109（韵达快递）加入队列  1-3
            if($v['f_company_id'] == '109' || $v['f_company_id'] == '125'){
                $companyId = $v['f_company_id'];
                //判断站点是否和快递合作  1-11
                $where['_string'] = "(f_station_id=".$v['f_station_id']." AND f_com_id=$companyId AND f_status='1')";
                $fId = D('tp_station_register')->where($where)->getField('f_id');
                if (!empty($fId)){
                    $abutmeng = D('Home/StationAbutmentLog')->where(array('f_parcel_id' => $v['f_parcel_id']))->find();
                    if(empty($abutmeng)){
                        $arr[$IndexArrNUM]['f_parcel_id'] = $v['f_parcel_id'];
                        $arr[$IndexArrNUM]['f_mail_no'] = $v['f_mail_no'];
                        $arr[$IndexArrNUM]['f_station_id'] = $v['f_station_id'];
                        $arr[$IndexArrNUM]['f_com_id'] = $v['f_company_id']; //请求快递公司ID
                        $arr[$IndexArrNUM]['f_com_name'] = $v['f_company_name'];
                        $arr[$IndexArrNUM]['f_station_name'] = $v['f_station_name'] ? $v['f_station_name'] : '';
                        $arr[$IndexArrNUM]['f_tel'] = $v['f_user_phone'] ? $v['f_user_phone'] : '';
                        $arr[$IndexArrNUM]['f_recipient_name'] = $v['f_recipient_name'] ? $v['f_recipient_name'] : '';
                        $arr[$IndexArrNUM]['f_create_time'] = date('YmdHis');
                        $arr[$IndexArrNUM]['f_type'] = 1;

                        $IndexArrNUM ++;
                    }
                }
            }
            //优速快递对接  1-13
            else if($v['f_company_id'] == '115'){
                $abutmeng = D('Home/StationAbutmentLog')->where(array('f_parcel_id' => $v['f_parcel_id']))->find();
                if(empty($abutmeng)){
                    $arr[$IndexArrNUM]['f_parcel_id'] = $v['f_parcel_id'];
                    $arr[$IndexArrNUM]['f_mail_no'] = $v['f_mail_no'];
                    $arr[$IndexArrNUM]['f_station_id'] = $v['f_station_id'];
                    $arr[$IndexArrNUM]['f_com_id'] = $v['f_company_id']; //请求快递公司ID
                    $arr[$IndexArrNUM]['f_com_name'] = $v['f_company_name'];
                    $arr[$IndexArrNUM]['f_station_name'] = $v['f_station_name'] ? $v['f_station_name'] : '';
                    $arr[$IndexArrNUM]['f_tel'] = $v['f_user_phone'] ? $v['f_user_phone'] : '';
                    $arr[$IndexArrNUM]['f_recipient_name'] = $v['f_recipient_name'] ? $v['f_recipient_name'] : '';
                    $arr[$IndexArrNUM]['f_create_time'] = date('YmdHis');
                    $arr[$IndexArrNUM]['f_type'] = 1;

                    $IndexArrNUM ++;
                }
            }
        }

        if (!empty($data)){
            D('Home/ParcelNoticeResult')->addAll($data);
        }
        if (!empty($arr)){
            D('Home/StationAbutmentLog')->addAll($arr);
        }
    }
    
}
